// pages/classifylist/classifylist.js
import {
  addCategory,
  delCategory
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    classArr: [],
    title: "", //分类名称
    memo: "", //备注
    isShowDel: false, //是否显示删除
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      classArr: app.globalData.itemList
    })
  },
  //显示删除分类按钮
  showDel: function() {
    this.setData({
      isShowDel: true
    })
  },
  //获取分类标题
  titleChange: function(e) {
    this.setData({
      title: e.detail.detail.value
    })

  },
  //获取备注
  memoChange: function(e) {
    this.setData({
      memo: e.detail.detail.value
    })
  },

  onPopupClose: function() {
    this.setData({
      show: false
    })

  },
  //点击添加分类
  onaddclassify: function() {
    this.setData({
      show: true
    })
  },
  //点击删除分类
  onDelClass: function(e) {
    let self = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除这个分类吗？',
      success: function(res) {
        if (res.confirm) {
          delCategory(e.currentTarget.dataset.id, (r) => {
            if (r.code == 0) {
              app.globalData.itemList = r.data;
              self.setData({
                classArr: r.data,
              })
            } else {
              errorMsg(r.message)
            }
          })
        } else if (res.cancel) {
          //点击取消
        }
      }
    })

  },
  //添加分类提交
  onAddCategorySubmit: function() {
    let {
      title,
      memo
    } = this.data;
    let self = this;

    addCategory(title, memo, (r) => {
      if (r.code == 0) {
        app.globalData.itemList = r.data;
        self.setData({
          classArr: r.data,
          show: false,
          title: "",
          memo: ""
        })

      } else {
        errorMsg(r.message);
      }
    })
  }
})